var allpagedata = new Array();
var screen_width = $(window).width();
var screen_height = $(window).height();
//alert(screen_width+'+'+screen_height);
//转发弹出框
var forwords_html = '<div class="forwords" style="padding:10px"><div class="close" title="关闭"><a href="" class="cancel"></a></div>';
forwords_html += '<div class="post_input ml30 mb10"><p class="mb5 mt5"style="font-size:16px;color:#555;">图片名：</p><input type="text" id="img_name" style="padding:5px;height:20px;line-height:20px;width:270px;font-size:14px;border:1px solid #bababa;color:#666;"></div>';
forwords_html += '<div class="post_input ml30 mb10"><p class="mb5"style="color:#555;">转至相册：</p><div class="temp_gallery"><a href="#" id="new_gallery">新建相册</a></div>';
forwords_html += '<div class="new_gallery none "><input type="text" id="gallery_name" style="height:20px;width:185px;padding:5px;border:1px solid #bababa;font-size:14px;color:#666;"><a href="#" id="create_gallery" class="ml5">&nbsp;&nbsp;新建</a><a href="#" id="no_new">&nbsp;&nbsp;取消</a></div></div>';
forwords_html += '<div class="care_button forwords_button" style="cursor:pointer;margin-top:20px;margin-left:30px">转&nbsp&nbsp&nbsp发</div><div class="forwords_img"></div></div>';

$(document).ready(function(){
	var strUrl=window.location.href;
	var arrUrl=strUrl.split("/");
	var strPage=arrUrl[arrUrl.length-1];
	if(strPage=="hot.php"){var request_type=2;var searchs = '';}
	if(strPage=="new.php"){var request_type=1;var searchs = '';}
	else{
		var ttt = strPage.split("?");
		if(ttt[0] == "search.php"){
			var request_type=4;var searchs = $("#search_add").text();
		}
	}
	
	
	var renum=1;
	
	var scrwidth = $(window).width();
	scrwidth -= 20;
	var scrw = parseInt(scrwidth/146);
	scrwidth = scrw*146;
	$("#stage").css("width",scrwidth);
	for(var i=0;i<scrw;i++){
		$("#stage").append("<li></li>");
	}
	loadMore(renum);
	

$(window).scroll(function(){
	// 当滚动到最底部以上100像素时， 加载新内容
	if ($(document).height() - $(this).scrollTop() - $(this).height()<100) loadMore(++renum);
});


function loadMore(num)
{

	$.ajax({
		url : "../data.php",
		data:{renum:num,request_type:request_type,searchs:searchs},
		type:"POST",
		dataType : 'json',
		success : function(json){
			if(typeof json == 'object'){
				var oProduct, $row, iHeight, iTempHeight,like;
				for(var i=0, l=json.length; i<l; i++){
					oProduct = json[i];					
					// 找出当前高度最小的列, 新内容添加到该列
					iHeight = -1;
					$('#stage li').each(function(){
						iTempHeight = Number( $(this).height() );
						if(iHeight==-1 || iHeight>iTempHeight){
							iHeight = iTempHeight;
							$row = $(this);
						}
					});
					//alert(iHeight);
					if(oProduct.like == 1){like = '<div class="action_not_like fl tc action_btn"><div class="heart"></div>取消</div>';}
					if(oProduct.like == 0){like = '<div class="action_like fl tc action_btn"><div class="heart"></div>喜欢</div>';}
					var temp='<div class="waterfall oh fl">';
					temp+='<div class="waterfall_img_warp oh pr">';
					temp+='<img src="'+oProduct.image+'" border="1" width="200" class="android_img"><div class="actions pa"><div class="action_comm fl tc action_btn"><div class="action_com"></div>评论</div>'+like+'<div class="action_repin fl tc action_btn"><div class="plus"></div>转发</div></div></div>';
					temp+='<div class="user_infos oh" style="padding:10px 15px;"><input type="hidden" class="this_id" value="'+oProduct.id+'"><input type="hidden" class="this_name" value="'+oProduct.title+'"><form class="waterfall_comm_form none"></form><div class="other_user_coment oh fl"><a href="user_album.php?pic='+oProduct.id+'" class="user_names dib">'+oProduct.puname+'</a>&nbsp&nbsp在&nbsp<a class="fb">'+oProduct.address+'</a>&nbsp发布&nbsp<a class="fb other_user_coments oh">'+oProduct.title+'</p></div></div>';
					if(oProduct.c1!=""){temp+='<div class="user_infos oh" style="padding:10px 15px 5px;"><img src="avatar.php?id='+oProduct.u1+'" alt="" class="user_heads fl db" /><a href="user_album.php?user='+oProduct.cu1+'" class="user_names dib">'+oProduct.n1+'</a><div class="other_user_coment oh fl"><p class="other_user_coments oh">'+oProduct.c1+'</p></div></div>';}
					if(oProduct.c2!=""){temp+='<div class="user_infos oh" style="padding:10px 15px 5px;"><img src="avatar.php?id='+oProduct.u2+'" alt="" class="user_heads fl db" /><a href="user_album.php?user='+oProduct.cu2+'" class="user_names dib">'+oProduct.n2+'</a><div class="other_user_coment oh fl"><p class="other_user_coments oh">'+oProduct.c2+'</p></div></div>';}
					if(oProduct.c3!=""){temp+='<div class="user_infos oh" style="padding:10px 15px 5px;"><img src="avatar.php?id='+oProduct.u3+'" alt="" class="user_heads fl db" /><a href="user_album.php?user='+oProduct.cu3+'" class="user_names dib">'+oProduct.n3+'</a><div class="other_user_coment oh fl"><p class="other_user_coments oh">'+oProduct.c3+'</p></div></div>';}
					temp+='</div>';
					$item=$(temp).hide();
					
					$row.append($item);
					$item.fadeIn();
					
					allpagedata.push(temp);
				}
			}
			
		
		
	},
	error:function(err){
		//alert(err);
		}
	});
	
}

			


});	

scrwidth = $(window).width();
setInterval(function(){
	scrwidth = wresize(scrwidth);
},50);

function wresize(scrwidth){
	//alert(scrwidth);
	var new_scrwidth = $(window).width();
	if(Math.abs(scrwidth - new_scrwidth) > 50){
		var scrolltop = $(document).scrollTop();
		var documentheight = $(document).height();
		scrwidth -= 20;
		var scrw_old = parseInt(scrwidth/240);
  	
  	$("#stage").empty();
  	var scrwidth = $(window).width();
		scrwidth -= 20;
		var scrw = parseInt(scrwidth/240);
		scrwidth = scrw*240;
		$("#stage").css("width",scrwidth);
		for(var i=0;i<scrw;i++){
			$("#stage").append("<li></li>");
		}
  	
  	var row,iHeight,iTempHeight;
  	for(var i=0, l=allpagedata.length; i<l; i++){
			iHeight = -1;
		$('#stage li').each(function(){
			iTempHeight = Number( $(this).outerHeight(true) );
			if(iHeight==-1 || iHeight>iTempHeight){
				iHeight = iTempHeight;
				row = $(this);
			}
		});
		
			var $item=$(allpagedata[i]).fadeTo(1,0);		
			row.append($item);
			$item.fadeTo(300,1);
		}
	setTimeout(function(){
		var new_scroll = $(document).height()*scrolltop/documentheight;
		//alert(scrolltop+"-"+documentheight+"-"+new_scroll);
		$("body,html").animate({scrollTop:new_scroll},100);
	},300);
	}
	return new_scrwidth;
}




//hover出现功能按钮
$('.waterfall_img_warp').live('hover',function(){
	$(this).find('.actions').stop(true,false).animate({'top':0},'fast');
});
$('.waterfall_img_warp').live('mouseout',function(){
	$(this).find('.actions').stop(false,true).animate({'top':-72},'fast');
});
					
//点击出现评论框
$('.action_comm').live('click',function(){
	var comm='<textarea class="waterfall_comm_box" id="waterfall_comm"></textarea><a href="#" id="submit_comm" class="water_sub dib">评论</a>';
	var temp=$(this);	
	$.ajax({url:"page.class.php?request=3",async:false,success:function(re){
		if(re=="yes"){
			temp.parent().parent().parent().find('.no_login').hide();
			$(".waterfall_comm_form:not(:empty)").hide(300).empty();		
			temp.parent().parent().parent().find('.waterfall_comm_form').append(comm).show(300).focus();
			setTimeout(function(){
				if($(".waterfall_comm_form:not(:empty)").offset().top - $(window).height() + 100 > $(window).scrollTop()){
				$("body,html").animate({scrollTop:$(".waterfall_comm_form:not(:empty)").offset().top + 100 - $(window).height()+"px"},300);
				}
			},300);
			$("#submit_comm").click(function(){
				if($("#waterfall_comm").val()!=""){							
					$.ajax({url:"page.class.php",data:{request:1,id:$(".waterfall_comm_form:not(:empty)").parent().find(".this_id").val(),comment:$("#waterfall_comm").val()},async:false,dataType:'json',success:function(re){
						var re_html='<div class="user_infos oh none" style="padding:10px 15px 5px;"><img src="avatar.php?id='+re[0].uid+'" alt="" class="user_heads fl db" /><a href="#" class="user_names dib">'+re[0].name+'</a><div class="other_user_coment oh fl"><p class="other_user_coments oh">'+$("#waterfall_comm").val()+'</p></div></div>';
						$(".waterfall_comm_form:not(:empty)").hide(300);
						$(".waterfall_comm_form:not(:empty)").parent().after(re_html);
						$(".waterfall_comm_form:not(:empty)").parent().next().show(300);
						//$(".waterfall_comm_form:not(:empty)").empty();
						//return false;
					}});
					}
					return false;						
			});
		}else if(re=="no"){
			showLogin($(top.document));
			}
		}});			
});

//点击喜欢按钮
$(".action_like").live('click',function(){
	var temp = $(this);
	var this_id = $(this).parent().parent().next().find(".this_id").val();
	$.ajax({url:"page.class.php?request=3",async:false,success:function(re){
		if(re=="yes"){
			$.ajax({url:"page.class.php?request=4&id="+this_id+"",async:false,success:function(re){
				temp.parent().find(".action_comm").after('<div class="action_not_like fl tc action_btn"><div class="heart"></div>取消</div>');
				temp.remove();
				if(re =="yes"){
					message("已喜欢");
				}else{
					message("操作失败,此图片您已标记喜欢");
				}
				}});
		}else if(re=="no"){
			showLogin($(top.document));
		}
	}});
});

//点取消喜欢
$(".action_not_like").live('click',function(){
	var temp = $(this);
	var this_id = $(this).parent().parent().next().find(".this_id").val();
	$.ajax({url:"page.class.php?request=3",async:false,success:function(re){
		if(re=="yes"){
			$.ajax({url:"page.class.php",data:{request:6,id:this_id},async:false,success:function(re){
				temp.parent().find(".action_comm").after('<div class="action_like fl tc action_btn"><div class="heart"></div>喜欢</div>');
				temp.remove();
				if(re =="yes"){
					message("已取消");
				}else{
					message("操作失败"+re);
				}
				}});
		}else if(re=="no"){
			showLogin($(top.document));
		}
	}});
});

//点击转发按钮
$(".action_repin").live('click',function(){
	var temp = $(this);
	var this_id = $(this).parent().parent().next().find(".this_id").val();
	var this_name = $(this).parent().parent().next().find(".this_name").val();
	$.ajax({url:"page.class.php?request=3",async:false,success:function(re){
		if(re=="yes"){
			$("body").append(forwords_html).append(opacity);
			$(".opacity").css({"width":$(window).width()+100+"px","height":$(window).height()+100+"px","position":"fixed","background-color":"#ccc","opacity":".8","left":"0px","top":"0px","z-index":"999"});
			$(".forwords").css({"z-index":"1000","position":"absolute","background-color":"#fff","left":$(window).width()/2-300+"px",height:"250px",top:$(top.window).scrollTop()-350+"px",width:"600px"});
			$("body").css("overflow","hidden");
			$.ajax({url:"user_info.class.php",data:{request:1},async:false,success:function(re){
				$(".temp_gallery").empty().html(re+'<a href="#" id="new_gallery" style="margin-left:8px;">新建相册</a>');
			}});
			$("#img_name").val(this_name);
			$(".forwords_img").append('<img src="image.php?id='+this_id+'&type=media">');
			$(".forwords_img img").css({"max-width":"230px","max-height":"210px",position:"absolute",top:"40px",right:"40px"});
			$(".forwords").animate({top:$(top.window).scrollTop()+"px"},300);
			$(".forwords_button").live('click',function(){
				$.ajax({url:"page.class.php",data:{request:5,id:this_id,name:$("#img_name").val(),gallery:$(".temp_gallery select").val()},type:"POST",async:false,success:function(re){
					if(re=="true"){
						$(".forwords").remove();
						$(".opacity").remove();
						message("转发成功");
					}
				}});
			});
			
		}else{
			showLogin($(top.document));
		}
	}});
});

	
//新建相册
$("#new_gallery").live('click',function(){
	$(".temp_gallery").hide();
	$(".new_gallery").show();
	return false;
});

$("#create_gallery").live('click',function(){
	if($("#gallery_name").val() != ""){
		$.ajax({url:"post_temp.class.php",type:"POST",data:{request:1,gallery_name:$("#gallery_name").val()},success:function(re){
			$(".temp_gallery").empty().html(re+'<a href="#" id="new_gallery">新建相册</a>');
			$(".new_gallery").hide();
			$(".temp_gallery").show();	
		}});
	}
	return false;
});

$("#no_new").live('click',function(){
	$(".temp_gallery").show();
	$(".new_gallery").hide();
	return false;
});
